package net.redlinesoft.app.iampetdo;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import net.redlinesoft.app.iampetdo.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.ads.*;

public class MainActivity extends Activity {
	ProgressBar mProgress;
	int mProgressStatus = 0;
	private AdView adView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		
		// Create the adView
		adView = new AdView(this, AdSize.BANNER, "a14fdcb18476694");
		// Lookup your LinearLayout assuming it’s been given
		// the attribute android:id="@+id/mainLayout"
		LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
		// Add the adView to it
		layout.addView(adView);
		// Initiate a generic request to load it with an ad
		adView.loadAd(new AdRequest());

		// play with progress bar
		mProgress = (ProgressBar) findViewById(R.id.progressBar);

		final WebView webView = (WebView) findViewById(R.id.webView);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setPluginsEnabled(true);
		webView.getSettings().setAllowFileAccess(true);
		webView.getSettings().setSupportZoom(true);
		// webView.getSettings().setBuiltInZoomControls(true);

		webView.setWebViewClient(new WebViewClient() {
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				// Handle the error
				webView.loadUrl("file:///android_asset/www/notconnect.html");
				Toast.makeText(getApplicationContext(), description,Toast.LENGTH_LONG).show();
			}
		});

		webView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});

		webView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				// Toast.makeText(getApplicationContext(),
				// "Loading " + progress + "% ...", Toast.LENGTH_SHORT)
				// .show();
				//mProgress.animate();
				mProgress.setProgress(progress);

			}
		});

		// check screen dimension
		//Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		//int width = display.getWidth();
		//int height = display.getHeight();

		//Toast.makeText(getApplicationContext(), width + "x" + height, Toast.LENGTH_LONG).show();

		if (this.chkStatus()) {
			webView.loadUrl("file:///android_asset/www/index.html");
		} else {
			webView.loadUrl("file:///android_asset/www/notconnect.html");
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//menu.add(R.string.menu_mobile);
		//menu.add(R.string.menu_donate);
		//menu.add(R.string.menu_about);
		//menu.add(R.string.menu_exit);
		menu.add(Menu.NONE,1,Menu.NONE,R.string.menu_donate);
		menu.add(Menu.NONE,4,Menu.NONE,R.string.menu_exit);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		/*
		 * exit
		 */
		if(item.getItemId() == 4){
    		this.finish();
    		return true;
    	}
		return false;
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// We do nothing here. We're only handling this to keep orientation
		// or keyboard hiding from causing the WebView activity to restart.
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		WebView webView = (WebView) findViewById(R.id.webView);
		if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
			webView.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public boolean chkStatus() {
		final ConnectivityManager connMgr = (ConnectivityManager) this
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		final android.net.NetworkInfo wifi = connMgr
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

		final android.net.NetworkInfo mobile = connMgr
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

		if (wifi.isAvailable()) {
			// Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
			return true;
		} else if (mobile.isAvailable()) {
			// Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
			return true;
		} else {
			Toast.makeText(this, "No Network ", Toast.LENGTH_LONG).show();
			return false;
		}
	}

}